package cn.kgc.springboot.exception;

import cn.kgc.springboot.util.ResponseResult;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.lang.model.element.UnknownAnnotationValueException;

/**
 * @author: hengge
 * @version: v1.0
 * @DATE: 2022/11/23
 * 全局异常处理类
 */
@RestControllerAdvice
public class GlobalException {


    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("操作失败");
        responseResult.setCode(500);
        return responseResult;
    }

    //(1)用户名错误
    @ExceptionHandler(UnknownAnnotationValueException.class)
    public ResponseResult  unknownAccountException(UnknownAccountException e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("用户名错误");
        responseResult.setCode(500);
        return  responseResult;
    }

    //(2)密码错误
    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResponseResult  incorrectCredentialsException(IncorrectCredentialsException e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("密码错误");
        responseResult.setCode(500);
        return  responseResult;
    }

    //(3)未授权异常: 当操作权限，提示该用户没有此权限
    @ExceptionHandler(UnauthorizedException.class)
    public ResponseResult unauthorizedException(UnauthorizedException e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("没有权限，请联系管理员");
        responseResult.setCode(500);
        return  responseResult;
    }

    //(4)锁定帐户异常
    @ExceptionHandler(LockedAccountException.class)
    public ResponseResult lockedAccountException(LockedAccountException e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("用户被锁定,请联系管理员");
        responseResult.setCode(500);
        return  responseResult;
    }

    //(5)验证码失效
    @ExceptionHandler(NullPointerException.class)
    public ResponseResult lockedAccountException(NullPointerException e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("验证码已失效");
        responseResult.setCode(500);
        return  responseResult;
    }
}
